package com.neu.flashmall.product.controller;

import com.neu.common.utils.Result;
import com.neu.flashmall.product.entity.PmsSpuImages;
import com.neu.flashmall.product.service.PmsSpuImagesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/pms_spuImage")
public class PmsSpuImagesController {
    @Resource
    private PmsSpuImagesService pmsSpuImagesService;

    @GetMapping("/getImageBySpu")
    public Result getImageBySpu(Long spuId){
        List<PmsSpuImages> all = pmsSpuImagesService.getImagesBySpu(spuId);
        return Result.ok().put(all);
    }

    @PutMapping("/updateImageUrl")
    public Result updateImageUrl(String url,Long spuId){
        String result = pmsSpuImagesService.updateUrl(url,spuId);
        if("更新URL失败".equals(result)){
            return Result.error().setMessage(result);
        }
        return Result.ok().setMessage(result);
    }

    @GetMapping("/updateName")
    public Result updateName(Long id,String name){
        String result = this.pmsSpuImagesService.updateName(name,id);
        if("数据库异常".equals(result)){
            return Result.error().setMessage(result);
        }
        return Result.ok().setMessage(result);
    }

    @GetMapping("/setDefault")
    public Result setDefault(Long id,Integer now,Long spuId){
        String result = this.pmsSpuImagesService.setDefault(now, id, spuId);
        if("数据库异常".equals(result)){
            return Result.error().setMessage(result);
        }
        return Result.ok().setMessage(result);
    }

    @DeleteMapping("/deleteImage")
    public Result deleteImage(Long id){
        String result = this.pmsSpuImagesService.delete(id);
        if("删除成功".equals(result)){
            return Result.ok().setMessage(result);
        }
        return Result.error().setMessage(result);
    }
}
